package UMC.Web;

import UMC.Data.Utility;
import UMC.Security.AccessToken;

import java.lang.reflect.Array;
import java.net.URI;

public class WebRequest {
    private WebClient client;
    private boolean isMaster, isWeiXin;

    protected WebRequest(String model, String cmd, WebMeta header, WebClient client) {
        this.client = client;
        if (Utility.isEmpty(client.UserAgent) == false) {
            isWeiXin = client.UserAgent.indexOf("MicroMessenger") > 10;
        }
        isMaster = client.accessToken.isInRole(AccessToken.AdminRole);

        _model = model;
        _cmd = cmd;

        this._Headers = new WebMeta(header);

    }

    String _model, _cmd;

    public String model() {
        return _model;
    }

    /// <summary>
    /// 提交的值
    /// </summary>
    public String sendValue() {
        Object v = this._Headers.get(this._cmd);
        if (v instanceof String) {
            return (String) v;
        }
        if (v != null) {
            if (v.getClass().isArray() && Array.getLength(v) > 0) {
                return (String) Array.get(v, 0);
            }
        }
        return null;

    }

    /// <summary>
    /// 提交的值
    /// </summary>
    public WebMeta sendValues() {

        return _Headers.meta(this._model);

    }

    public String userHostAddress() {
        return this.client.UserHostAddress;
    }

    WebMeta Arguments = new WebMeta();

    public WebMeta arguments() {
        return Arguments;
    }

    WebMeta _Headers;// = new WebMeta();

    public WebMeta headers() {
        return _Headers;
    }


    public String cmd() {
        return _cmd;
    }

    public boolean isCashier() {
        return this.client.isCashier;
    }

    public boolean isMaster() {
        return isMaster;
    }

    public boolean isWeiXin() {
        return isWeiXin;
    }

    public String userAgent() {
        return client.UserAgent;
    }

    public boolean isApp() {
        return this.client.isApp;
    }

    public URI uri() {
        return client.Uri;
    }

    public URI referrer() {
        return client.UrlReferrer;
    }

}
